## Locations
SRCDIR=src
outdir=out
distdir=dist

## Extra headers for generated HTML
rss_header='<link rel="alternate" type="application/rss+xml" title="RSS Feed" href="rss.xml" />'
atom_header='<link rel="alternate" type="application/atom+xml" title="Atom Feed" href="atom.xml" />'
extra_headers=$(rss_header)$(atom_header)$(track_header)

## Files
_STATIC_FILES=$(shell find static -type f -name \*)
STATIC_OUT=$(patsubst static/%,out/%,$(_STATIC_FILES))

_PAGES=$(shell find src/*.texi)
BLOG_PAGES=$(patsubst src/%.texi,%.html,$(_PAGES))

LATEST_FILE=$(shell ls -1 src/posts/*-*-*.texi|tail -1)
LATEST_NODE=$(shell awk '/@node / {$$1=""; gsub(/^ /,""); print $$0}' ${LATEST_FILE})
LATEST_DATE=$(shell grep @date ${LATEST_FILE}|cut -d" " -f 2)

#POSTS=$(shell find src/posts/*.texi)
_POSTS=$(shell ls -1r src/posts/*.texi | tr \\n ,)
POSTS=$(shell ls -rm src/posts/*texi)
#POSTS_PAGES=$(patsubst src/%.texi,%.html,$(_POSTS))
#POSTS=$(wildcard src/*.texi)

HTML_POSTS=$(shell for f in out/*html; do awk -v _fname=$$f '/id="_article_date"/ { if (match($$0, /_article_date">(.*)</, a)) print a[1] " " _fname}' $$f; done|sort -r|awk '{print $$2}'|tr \\n ,)

## Rules

.PHONY: all test posts.texi info last-update dist nav

all:	src/posts.texi src/config.texi src/index.html $(STATIC_OUT) atom.xml rss.xml nav

test:
	@echo EXTRA_HEAD=$(extra_headers)
# -c TOC_LINKS=0
src/index.html: src/posts.texi src/config.texi
	@texi2any  -c USE_XML_SYNTAX=0 -c SIMPLE_MENU=0 -c MENU_SYMBOL='&#9642;&nbsp;' -c COPIABLE_LINKS=1 \
	-c AVOID_MENU_REDUNDANCY=0 -c TOC_LINKS=0 -c NODE_NAME_IN_INDEX=0 -c NODE_NAME_IN_MENU=1 -c SECTION_NAME_IN_TITLE=1 -c FORMAT_MENU="menu" \
	-c MENU_ENTRY_COLON=" " -c CHECK_NORMAL_MENU_STRUCTURE=1 -c PROGRAM_NAME_IN_FOOTER=1 -c SHOW_TITLE=1 -c DATE_IN_HEADER=1 \
        -c NO_CUSTOM_HTML_ATTRIBUTE=1 -c EXTRA_HEAD=$(extra_headers) --html --css-ref=styles/brutex.css \
	-c HIGHLIGHT_SYNTAX="pygments" -c WORDS_IN_PAGE=3000 -c CONTENTS_OUTPUT_LOCATION="separate_element" \
	--no-number-sections -o $(outdir) -I static/ --internal-links=out/int-links.txt src/index.texi

out/%: static/%
	@mkdir -p $(@D)
	@cp -r $< $@

src/config.texi: src/config.texi.m4 src/config.m4
#	@echo Latest: ${LATEST_FILE} ${LATEST_DATE} "${LATEST_NODE}"
	@m4 -D__latest_date=${LATEST_DATE} -D__latest_file=${LATEST_FILE} -D__latest_node="${LATEST_NODE}" -I src -I src/lib $< > $@

clean:
	@echo "Removed output dir"
	@rm -rf $(outdir) $(distdir)
	@echo "Removed generated files"
	@rm -f src/config.texi src/posts.texi

info:   src/posts.texi src/config.texi
	@texi2any -I static/ src/index.texi -o out/website.info

txt:   src/posts.texi src/config.texi
	@texi2any -I static/ src/index.texi --plaintext -o out/website.txt

pdf:	src/posts.texi src/config.texi
	@texi2any -I static/ src/index.texi --pdf -o out/website.pdf

dist:	all
	@mkdir -p $(distdir)
	@tar zcvf $(distdir)/website.tar.gz $(outdir)

rss.xml: src/rss.m4 src/index.html
	@m4 -D__html_files=$(HTML_POSTS) -I src -I src/lib $< > out/$@

atom.xml: src/atom.m4 src/index.html
	@m4 -D__html_files=$(HTML_POSTS) -I src -I src/lib $< > out/$@

src/posts.texi: src/posts.m4
	@m4 -D__post_files="$(POSTS)" -I src -I src/lib $< > $@

## Add a "Top" navigation button. This should be doable using texi2any
## (which would be better)
nav:	src/index.html
	@awk  -i inplace '{ if (match($$ 0,/\[.*Contents.*\]\[.*Index.*\]/,a)) { gsub(/<\/p>/,"[<a href=\"index.html\">Top</a>] </p>"); print } else { print }}' $(outdir)/*html
